#!/bin/bash
# Normalize hosts file.
# https://github.com/racaljk/hosts

date_amend()
{
	# check if hosts file changes.
	if git diff --exit-code "$1" > /dev/null; then
		# hosts file has not been modified, change the date
		# to last commit date.
		local commit_date=$(git log --pretty=format:"%cd" --date=short -1 "$1")

		perl -i -pe "s|(?<=Last updated: )\d{4}-\d{2}-\d{2}|$commit_date|g" "$1"
	else
		# hosts file has been modified but not committed,
		# change the date to system date.
		perl -i -pe "s|(?<=Last updated: )\d{4}-\d{2}-\d{2}|$(date +%F)|g" "$1"
	fi
}

eol_amend()
{
	# In case people don't have dos2unix.
	cat "$1" | tr -d '\r' > h.swp
	mv -f h.swp "$1"
}

style_amend()
{
	perl -i -pe "s|((\d+\.){3}\d+)[ \t]+|\1\t|g" "$1"
	# Remove leading and trailing whitespace.
	perl -i -pe "s|^[ \t]+\|[ \t]+$||g" "$1"
}

if [ -z "$1" ]; then
	echo "Usage: $0 [hosts-file]"
	exit 1
fi

eol_amend "$1"
date_amend "$1"
style_amend "$1"
